<?php
namespace App\Http\Controllers;

use App\Http\Requests\SmsTemplateRequest;
use App\Models\ActivityApply;
use App\Models\SmsTemplate;
use App\Models\Zone;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SmsTemplateController extends CommonController
{

    /**
     * 短信模板管理界面
     */
    public function getIndex()
    {
        $lists = SmsTemplate::orderBy('id', 'desc')->paginate(10);

        return view('smstemplate.index')->with('lists', $lists)->with('actions', $this->actions);
    }

    public function search(Request $request)
    {
        $data = $request->input();

        $lists = SmsTemplate::orderBy('id', 'asc');
        if (isset($data['subject']) && $data['subject'] != '') {
            $lists->where('subject', 'like', '%' . $data['subject'] . '%');
        }

        if (isset($data['content']) && $data['content'] != '') {
            $lists->where('content', 'like', '%' . $data['content'] . '%');
        }

        if (isset($data['related_project']) && $data['related_project'] != '') {
            $lists->where('related_project', $data['related_project']);
        }

        if (isset($data['related_activity']) && $data['related_activity'] != '') {
            $lists->where('related_activity', $data['related_activity']);
        }


        $lists = $lists->paginate(10);

        return view('smstemplate.search')->with("lists", $lists)->with("actions", $this->actions)
            ->with('subject', $data['subject'])->with('content', $data['content'])
            ->with('related_project', $data['related_project'])->with('related_activity', $data['related_activity']);
    }

    public function getCreate()
    {
        return view('smstemplate.create');
    }

    public function postCreate(SmsTemplateRequest $request)
    {
        $data = $request->input();
        if (strstr($data['content'], '::activity') && $data['related_activity'] == 0) {
            return AjaxCallbackMessage('搜索到模板中含有活动占位符，但是你却选择了，活动不关联，这将导致问题，请确认', false);
        }

        if (!strstr($data['content'], '::activity') && $data['related_activity'] == 1) {
            return AjaxCallbackMessage('搜索到模板中没有活动占位符，但是你却选择了，活动关联，这将导致问题，请确认', false);
        }

        if (strstr($data['content'], '::project') && $data['related_project'] == 0) {
            return AjaxCallbackMessage('搜索到模板中含有项目占位符，但是你却选择了，项目不关联，这将导致问题，请确认', false);
        }

        if (strstr(!$data['content'], '::project') && $data['related_project'] == 1) {
            return AjaxCallbackMessage('搜索到模板中没有项目占位符，但是你却选择了，项目关联，这将导致问题，请确认', false);
        }

        unset($data['formType'], $data['_token']);
        SmsTemplate::create($data);

        return AjaxCallbackMessage('创建成功', true, url('smstemplate/index'));
    }

    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = SmsTemplate::where('id', $data['id'])->first();

        return view('smstemplate.edit')->with('list', $list);
    }

    public function postEdit(SmsTemplateRequest $request)
    {
        $data = $request->input();
        if (strstr($data['content'], '::activity') && $data['related_activity'] == 0) {
            return AjaxCallbackMessage('搜索到模板中含有活动占位符，但是你却选择了，活动不关联，这将导致问题，请确认', false);
        }

        if (!strstr($data['content'], '::activity') && $data['related_activity'] == 1) {
            return AjaxCallbackMessage('搜索到模板中没有活动占位符，但是你却选择了，活动关联，这将导致问题，请确认', false);
        }

        if (strstr($data['content'], '::project') && $data['related_project'] == 0) {
            return AjaxCallbackMessage('搜索到模板中含有项目占位符，但是你却选择了，项目不关联，这将导致问题，请确认', false);
        }

        if (strstr(!$data['content'], '::project') && $data['related_project'] == 1) {
            return AjaxCallbackMessage('搜索到模板中没有项目占位符，但是你却选择了，项目关联，这将导致问题，请确认', false);
        }

        unset($data['formType'], $data['_token']);
        SmsTemplate::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('smstemplate/index'));
    }
}